import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:hc_bot_pro/common/tool/string_ex.dart';

import '../common/routes.dart';
import 'index.dart';
import 'widgets/cell.dart';

class CurePage extends GetView<CureController> {
  const CurePage({super.key});

  // 主视图
  Widget _buildView() {
    final cures = controller.cures;
    return GridView.count(
      crossAxisSpacing: 10.w,
      mainAxisSpacing: 10.h,
      crossAxisCount: 5,
      children: cures.map((e) => Cell(cure: e)).toList(),
    ).paddingSymmetric(horizontal: 10.sp, vertical: 10.sp);
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<CureController>(
      init: CureController(),
      id: "cure",
      builder: (_) {
        return Theme(
          data: ThemeData(
            appBarTheme: AppBarTheme(
              centerTitle: true,
              color: Colors.transparent,
              titleTextStyle: TextStyle(
                fontSize: 15.sp,
                color: controller.channel == null
                    ? Colors.black
                    : controller.channel!.appStyle.colors.pickTitleColor
                          .toColor(),
              ),
            ),
          ),
          child: Container(
            decoration: BoxDecoration(
              image: DecorationImage(
                fit: BoxFit.cover,
                image: controller.bgUrl.isEmpty
                    ? const AssetImage("assets/images/bg_main.png")
                    : CachedNetworkImageProvider(controller.bgUrl),
              ),
            ),
            child: Scaffold(
              backgroundColor: Colors.transparent,
              appBar: AppBar(
                title: const Text("选择您的当下感受，体验前所未有的身心疗愈"),
                actions: [
                  IconButton(
                    onPressed: () {
                      controller.onRefreshData();
                    },
                    icon: Icon(Icons.refresh),
                  ),
                ],
              ),
              body: SafeArea(child: _buildView()),
            ),
          ),
        );
      },
    );
  }
}
